//
//  CYMinePropertyViewController.m
//  cyjf
//
//  Created by tauCross on 2017/5/17.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYMinePropertyViewController.h"

#import "CYMinePropertyTopCell.h"
#import "CYMinePropertyCell.h"
#import "CYEmptyCell.h"

#import "CYBillViewController.h"
#import "CYWebViewController.h"



@interface CYMinePropertyViewController ()

@property(nonatomic, strong)CYMinePropertyTopCellObject *topCell;
@property(nonatomic, strong)CYMinePropertyCellObject *availableCell;
@property(nonatomic, strong)CYMinePropertyCellObject *withdrawCell;
@property(nonatomic, strong)CYMinePropertyCellObject *waitCapitalCell;
@property(nonatomic, strong)CYMinePropertyCellObject *waitInterestCell;
@property(nonatomic, strong)CYMinePropertyCellObject *totalInvestCell;

@property(nonatomic, strong)UIView *footerView;
@property(nonatomic, strong)UILabel *footerLabel;
@property(nonatomic, strong)UIImageView *footerIcon;
@property(nonatomic, strong)UIButton *vipButton;

@end


@implementation CYMinePropertyViewController

- (void)close
{
    [MobClick event:UM_EVENT_0052];
    [super close];
}

- (void)loadUI
{
    self.navigationItem.title = @"我的资产";
    
    [self loadCells];
    
    self.footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 102)];
    self.tableView.tableFooterView = self.footerView;
    {
        self.footerLabel = [[UILabel alloc] init];
        [self.footerView addSubview:self.footerLabel];
        
        self.footerIcon = [[UIImageView alloc] initWithImage:CYImage(@"ico_question_blue")];
        [self.footerView addSubview:self.footerIcon];
        
        self.vipButton = [[UIButton alloc] init];
        [self.vipButton setTitle:@"什么是VIP投资特权" forState:UIControlStateNormal];
        self.vipButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [self.vipButton setTitleColor:HEXCOLOR(@"3E8CE6") forState:UIControlStateNormal];
        [self.vipButton addTarget:self action:@selector(onVIPButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [self.footerView addSubview:self.vipButton];
    }
}

- (void)loadData
{
    self.topCell.totalProperty = self.totalProperty;
    
    self.availableCell.content = [SAFE_NUMBER(self.available) amountString];
    
    self.withdrawCell.content = [SAFE_NUMBER(self.withdraw) amountString];
    
    self.waitCapitalCell.content = [SAFE_NUMBER(self.waitCapital) amountString];
    
    self.waitInterestCell.content = [SAFE_NUMBER(self.waitInterest) amountString];
    
    self.totalInvestCell.content = [SAFE_NUMBER(self.totalInvest) amountString];
    
    if(self.totalInvest.doubleValue >= self.vipInvest.doubleValue)
    {
        self.footerLabel.attributedText = [[NSAttributedString alloc] initWithString:@"您已享受VIP投资特权服务" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSForegroundColorAttributeName : HEXCOLOR(@"999999")}];
    }
    else
    {
        self.footerLabel.attributedText = ({
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
            [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"再投" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSForegroundColorAttributeName : HEXCOLOR(@"999999")}]];
            [string appendAttributedString:[[NSAttributedString alloc] initWithString:@(self.vipInvest.doubleValue - self.totalInvest.doubleValue).amountString attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSForegroundColorAttributeName : HEXCOLOR(@"0E8CED")}]];
            [string appendAttributedString:[[NSAttributedString alloc] initWithString:@"元即可享受VIP投资特权服务" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSForegroundColorAttributeName : HEXCOLOR(@"999999")}]];
            string;
        });
    }
    
    [self.tableView reloadData];
}

- (void)layoutUI
{
    [self.footerLabel sizeToFit];
    [self.vipButton sizeToFit];
    self.vipButton.height = 44;
    if(self.totalInvest.doubleValue >= self.vipInvest.doubleValue)
    {
        self.footerLabel.left = 20;
        self.footerLabel.centerY = 26;
        
        self.footerIcon.centerY = 26;
        self.footerIcon.right = self.footerView.width - 20 - self.vipButton.width - 5;
        
        self.vipButton.right = self.footerView.width - 20;
        self.vipButton.centerY = self.footerIcon.centerY;
    }
    else
    {
        self.footerLabel.centerY = 26;
        self.footerLabel.centerX = self.footerView.width / 2;
        
        CGFloat width = self.footerIcon.width + 5 + self.vipButton.width;
        self.footerIcon.left = (self.footerView.width - width) / 2;
        self.footerIcon.centerY = 53;
        
        self.vipButton.centerY = 53;
        self.vipButton.left = self.footerIcon.right + 5;
    }
}

#pragma mark - Private Mehtod
- (void)loadCells
{
    self.topCell = [CYMinePropertyTopCellObject objectWithCellClass:[CYMinePropertyTopCell class]];
    
    self.availableCell = [CYMinePropertyCellObject objectWithCellClass:[CYMinePropertyCell class]];
    self.availableCell.title = @"可用余额";
    
    self.withdrawCell = [CYMinePropertyCellObject objectWithCellClass:[CYMinePropertyCell class]];
    self.withdrawCell.title = @"提现中金额";
    
    self.waitCapitalCell = [CYMinePropertyCellObject objectWithCellClass:[CYMinePropertyCell class]];
    self.waitCapitalCell.title = @"待收本金";
    
    self.waitInterestCell = [CYMinePropertyCellObject objectWithCellClass:[CYMinePropertyCell class]];
    self.waitInterestCell.title = @"待收收益";
    
    self.totalInvestCell = [CYMinePropertyCellObject objectWithCellClass:[CYMinePropertyCell class]];
    self.totalInvestCell.title = @"累计投资金额";
    
    NSMutableArray *cells = [NSMutableArray array];
    {
        [cells addObject:self.topCell];
    }
    {
        CYEmptyCellObject *cell = [CYEmptyCellObject objectWithCellClass:[CYEmptyCell class]];
        cell.height = 10;
        [cells addObject:cell];
    }
    {
        [cells addObject:self.availableCell];
    }
    {
        [cells addObject:self.withdrawCell];
    }
    {
        [cells addObject:self.waitCapitalCell];
    }
    {
        [cells addObject:self.waitInterestCell];
    }
    {
        CYEmptyCellObject *cell = [CYEmptyCellObject objectWithCellClass:[CYEmptyCell class]];
        cell.height = 10;
        [cells addObject:cell];
    }
    {
        [cells addObject:self.totalInvestCell];
    }
    self.tableModel = [[NIMutableTableViewModel alloc] initWithSectionedArray:cells delegate:(id)[NICellFactory class]];
    self.tableView.dataSource = self.tableModel;
}

#pragma mark - Control Event
- (void)onVIPButtonTouchUpInside
{
    CYWebViewController *webViewController = [[CYWebViewController alloc] init];
    webViewController.url = [CY_H5_BASE_URL stringByAppendingString:@"views/vip.html"];
    [self.navigationController pushViewController:webViewController animated:YES];
}

@end
